package org.holynite.fieldmanage.managed.backing

import org.holynite.fieldmanage.managed.model.CustomerTypeFormBean
import org.holynite.fieldmanage.managed.model.CustomerTypesBean

import javax.enterprise.context.RequestScoped
import javax.faces.bean.ManagedBean
import javax.faces.bean.ManagedProperty
import javax.faces.event.ValueChangeEvent

/**
 * Created by midnite on 15/1/31.
 */
@ManagedBean
@RequestScoped
class CustomerTypeSelectBean implements Serializable {

    @ManagedProperty("#{customerTypeFormBean}")
    CustomerTypeFormBean ctForm
    @ManagedProperty("#{customerTypesBean}")
    CustomerTypesBean ctBean
    
    void loadCustomerTypeDetail(ValueChangeEvent event) {
        def selected = event.newValue as String
        if (selected.equals("new_content")) {
            ctForm.addMode = true
            ctForm.clearForm()
        } else {
            def customerType = ctBean.findCustomerType(selected)
            ctForm.typeName = customerType.customerTypeName
            ctForm.precharged = customerType.precharged
            ctForm.multipleAllowed = customerType.multipleChargeAsSingle
            ctForm.discount = customerType.discount
            ctForm.selectedCharge = customerType.chargeType.codeSign
            ctForm.selectedUsable = customerType.usables.codeSign
        }
    }
}
